import json
from json import JSONDecodeError

from flask import current_app

from app.common.common_response_code import response_code
from app.common.common_response_log import ResponseLog


class requestProcess(object):

    def _json_request(self, request):
        """
        处理json请求参数问题
        :param request: 请求
        :return:
        """
        try:
            request_data = request.data
            req_str = request_data.decode()
            if req_str == '':
                return {}
            data = json.loads(req_str)
            if isinstance(data, list):
                return False
            return data
        except JSONDecodeError as e:
            current_app.logger.error(e)
            return False

    def request_process(self, request):
        """
        请求参数获取
        :param request: 请求
        :param xml:  请求响应类型,默认是json
        :return:
        """
        return self._json_request(request)

    def verify_one_param_type(self, param_name, value, type=None):
        """
        验证某个参数的类型
        :param param_name: 验证的参数名称
        :param value: 验证的参数的值
        :param type: 验证的参数的类型
        :return:
        """
        try:
            if type == float:
                v = None
                if isinstance(value, str):
                    v = eval(value)
                if isinstance(value, int):
                    v = value
                if isinstance(value, float):
                    v = value
                if isinstance(v, float):
                    pass
                else:
                    code = response_code.BAD_REQUEST
                    code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
                    return code
            if type == int:
                v = None
                if isinstance(value, str):
                    v = eval(value)
                if isinstance(value, float):
                    v = value
                if isinstance(value, int):
                    v = value
                if isinstance(v, int):
                    pass
                else:
                    code = response_code.BAD_REQUEST
                    code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
                    return code
            if type == str:
                if isinstance(value, str):
                    pass
                else:
                    code = response_code.BAD_REQUEST
                    code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
                    return code

            if type == list:
                if isinstance(value, list):
                    pass
                else:
                    code = response_code.BAD_REQUEST
                    code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
                    return code
            if type == dict:
                if isinstance(value, dict):
                    pass
                else:
                    code = response_code.BAD_REQUEST
                    code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
                    return code
        except Exception as e:
            current_app.logger.error(e)
            code = response_code.BAD_REQUEST
            code['msg'] = ResponseLog.wrong_param_type(param_name, type.__name__)
            return code

    def verify_one_param_must(self, request_data: dict, param):
        """
        验证某个参数是否必填
        :param data: 请求的数据
        :param param: 本验证的字段
        :return:
        """
        if request_data.get(param) is None:
            code = response_code.BAD_REQUEST
            code['msg'] = ResponseLog.wrong_param_must(param)
            return code
        else:
            pass

    def verify_all_param_must(self, request_data: dict, fields: list):
        """
        批量验证是否是必传参数
        :param request_data: 请求的参数数据
        :param fields: ['a','b']
        :return:
        """
        for i in fields:
            must = self.verify_one_param_must(request_data, i)
            if must:
                return must
            else:
                pass

    def verify_all_param_type(self, request_data: dict, fields: dict):
        """
        批量验证参数的类型
        :param request_data: 请求的参数数据
        :param fields: {'a':str,'b':int}
        :return:
        """

        for k, v in request_data.items():
            param_type = self.verify_one_param_type(k, v, fields.get(k))
            if param_type:
                return param_type
            else:
                pass


req = requestProcess()
